using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ProductParamDAL
    {
        public static int Save(ProductParamInfo info)
        {
            string sql = string.Format("insert into [productparam]([AdminID],[ProductID],[ParentID],[Name],[Value],[Sort],[Type]) values(@AdminID,@ProductID,@ParentID,@Name,@Value,@Sort,@Type);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@ParentID", info.ParentID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Value", info.Value),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Type", info.Type)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(ProductParamInfo info)
        {
            string sql = string.Format("update [productparam] set [AdminID]=@AdminID,[ProductID]=@ProductID,[ParentID]=@ParentID,[Name]=@Name,[Value]=@Value,[Sort]=@Sort,[Type]=@Type where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@ParentID", info.ParentID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Value", info.Value),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Type", info.Type),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static ProductParamInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[productparam]");
            return SimpleData(dr);
        }
        public static List<ProductParamInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[productparam]");
            return ListSimpleData(dt);
        }
        public static List<ProductParamInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [productparam].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [productparam].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[productparam]", out count);
            return ListSimpleData(dt);
        }
        public static List<ProductParamInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [productparam]"));
            return ListSimpleData(dt);
        }
        public static bool DeleteByProductID(int adminid)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("delete from [productparam] where AdminID={0} and ProductID=0",adminid));
        }
        public static bool UpdateProductParamName(int adminid, int id, string name)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [productparam] set [Value]='{0}' where AdminID={1} and ID={2}", name, adminid, id));
        }
        public static bool UpdateProductID(int adminid, int productid)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [productparam] set [productid]={0} where AdminID={1} and ProductID=0", productid, adminid));
        }
        public static List<ProductParamInfo> FindByProductID(int adminid, int productid)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [productparam].ProductID ={0}", productid);
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [productparam] where AdminID={0} {1} order by Sort", adminid, filter.ToString()));
            return ListSimpleData(dt);
        }
        private static ProductParamInfo SimpleData(DataRow dr)
        {
            ProductParamInfo info = new ProductParamInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.ParentID = DataConvert.ToInt(dr["ParentID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Value = DataConvert.ToString(dr["Value"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
            }
            return info;
        }
        private static ProductParamInfo AllData(DataRow dr)
        {
            ProductParamInfo info = new ProductParamInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.ParentID = DataConvert.ToInt(dr["ParentID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Value = DataConvert.ToString(dr["Value"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
            }
            return info;
        }
        private static List<ProductParamInfo> ListSimpleData(DataTable dt)
        {
            List<ProductParamInfo> list = new List<ProductParamInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ProductParamInfo> ListAllData(DataTable dt)
        {
            List<ProductParamInfo> list = new List<ProductParamInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

